﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace JoomlaXMLEditor
{
    // okno parametrów szablonu
    public partial class TemplateParameters : Form
    {
        // uchywt do elementu rodzica
        public MainWindow parent;
        // lista węzłów parametrów
        public List<XmlNode> parameters;
        // konstruktor
        public TemplateParameters(MainWindow parent)
        {
            InitializeComponent();

            this.parent = parent;

            this.parameters = new List<XmlNode>();

            foreach (XmlNode node in this.parent.actualFile.GetElementsByTagName("params")[0].ChildNodes)
            {
                this.parameters.Add(node.CloneNode(true));

                if (node.Attributes["name"] != null)
                {
                    this.listBox1.Items.Add(node.CloneNode(true).Attributes["name"].Value);
                }
            }
        }
        // zamykanie
        private void button7_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // przesuwanie w górę
        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > 0)
            {
                XmlNode tmpl1 = this.parameters[listBox1.SelectedIndex];
                this.parameters[listBox1.SelectedIndex] = this.parameters[listBox1.SelectedIndex - 1];
                this.parameters[listBox1.SelectedIndex - 1] = tmpl1;

                object tmpl2 = this.listBox1.Items[listBox1.SelectedIndex];
                this.listBox1.Items[listBox1.SelectedIndex] = this.listBox1.Items[listBox1.SelectedIndex - 1];
                this.listBox1.Items[listBox1.SelectedIndex - 1] = tmpl2;

                this.listBox1.SelectedIndex = listBox1.SelectedIndex - 1;
            }
        }
        // przesuwanie w dół
        private void button5_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
            {
                XmlNode tmpl1 = this.parameters[listBox1.SelectedIndex];
                this.parameters[listBox1.SelectedIndex] = this.parameters[listBox1.SelectedIndex + 1];
                this.parameters[listBox1.SelectedIndex + 1] = tmpl1;

                object tmpl2 = this.listBox1.Items[listBox1.SelectedIndex];
                this.listBox1.Items[listBox1.SelectedIndex] = this.listBox1.Items[listBox1.SelectedIndex + 1];
                this.listBox1.Items[listBox1.SelectedIndex + 1] = tmpl2;

                this.listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
            }
        }
        // usuwanie
        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                this.parameters.RemoveAt(listBox1.SelectedIndex);
                this.listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
        }
        // zapisywanie
        private void button6_Click(object sender, EventArgs e)
        {
            this.parent.actualFile.GetElementsByTagName("params")[0].RemoveAll();

            foreach (XmlNode node in this.parameters)
            {
                this.parent.actualFile.GetElementsByTagName("params")[0].AppendChild(node);
            }

            new TemplateXML(this.parent).refreshTree(this.parent.XMLTree, this.parent.actualFile);

            this.Close();
        }
        // dodawanie
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBox1.Text == "")
            {
                MessageBox.Show("Proszę wybrać typ pola do dodania");
            }
            else
            {
                new TemplateElementEditor(this.comboBox1.Text, this, -1).Show();
            }
        }
        // edycja
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex >= 0)
            {
                TemplateElementEditor editor = new TemplateElementEditor(null, this, this.listBox1.SelectedIndex);
                editor.Show();
            }
            else
            {
                MessageBox.Show("Proszę wybrać element do edycji");
            }
        }
    }
}
